<template>
    <div class="container">
        <!-- 登录导航 -->
<el-menu
  class="el-menu-demo"
  mode="horizontal"
  background-color="#545c64"
  text-color="#fff"
  active-text-color="#ffd04b">
  <el-menu-item index="1">宠物首页</el-menu-item>
  <el-submenu index="2">
    <template slot="title">用户中心</template>
    <el-menu-item index="2-1">登录</el-menu-item>
    <el-menu-item index="2-2">注册</el-menu-item>
   </el-submenu>
</el-menu>
  <div class="bgimg">
        <!-- 登录页 -->
        <div class="user-rge">
     <div class="reg-bt">登录</div>
    <div class="user-form">
      <el-form  style="width: 350px"
       label-width="100px"
       :rules="rules" 
       :model="form"
       rules:rules
       >
      <el-form-item label="用户名" prop="uname" label-color="#fff">
        <el-input v-model="form.uname" />
      </el-form-item>
      <el-form-item label="密码" prop="upwd">
        <el-input  show-password  v-model="form.upwd"/>
      </el-form-item>
     
      <el-form-item>
        <el-button  type="primary" @click="login">立即登录</el-button>
      </el-form-item>
    </el-form>
             </div>
        </div>
  </div>
    </div>
</template>

<script>
    export default {
        data() {

    return {
      form: {
        uname: '',
        upwd: '',
      },
      rules: {  // 定义验证规则
        uname: [
          {required:true,message:'用户名必填',trigger:'blur'},
          {min:3,max:15,message:'长度：3~15',trigger:'blur'}
        ],
        upwd:[
          {required:true,message:'密码必填',trigger:'blur'},
          {pattern:/^\d{6}$/,message:'必须是6位数字',trigger:'blur'}
        ]
      }
    }
  }, 
  methods:{
    login(){
      let url='v1/user/login'
      let params=`uname=${this.form.uname}&upwd=${this.form.upwd}`
      this.axios.post(url,params).then(res=>{
        console.log(res);
        if(res.data.code==200){
          //触发updateUname方法，把用户名保存共享
            //
            this.$store.commit('updateUname',this.form.uname)
            this.$router.push('/')
        }else{
          alert('用户名或者密码错误')
        }
      })
    }
  }
    }
</script>

<style lang="scss" scoped>
</style>
<style  src="../assets/css/login.css">

</style>